﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FisheryPlatform.Bussiness.WebRuntime;
using FisheryPlatform.Bussiness;
using FisheryPlatform.Bussiness.Product;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.Cache;

namespace FisheryPlatform.Web.Admin.Product
{
    /// <summary>
    /// Action 的摘要说明
    /// </summary>
    public class Action : HttpHandlerBase, IHttpHandler
    {
        private long _ProductId = 0;
        private ProductActionResult actionResult = new ProductActionResult();
        public void ProcessRequest(HttpContext context)
        {
            #region
            base.Init(context);
            try
            {
                if (string.IsNullOrEmpty(FormValue("actionType")) == false
                    && string.IsNullOrEmpty(FormValue("proId")) == false)
                {
                    _ProductId = long.Parse(FormValue("proId"));
                    switch (FormValue("actionType"))
                    {
                        case "1": SetProductStatus(); break;
                        default: actionResult.message = "无法确定动作类型"; break;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage.Record(ex);
                actionResult.success = false;
                actionResult.message = "系统报错";
            }

            SerializeDataToResponseStream(actionResult);
            #endregion
        }

        private void SetProductStatus()
        {
            #region
            var opType = FormValue("opType") ?? "-1";
            DateTime createDate = DateTime.Parse(FormValue("CreateDate") ?? DateTime.Now.ToString());
            if (opType == "1")
            {//上线
                actionResult.success = ProductManager.UpdateStatus(_ProductId, ProductStatusEnum.Publishing);
            }
            else if (opType == "2")
            {//下线
                actionResult.success = ProductManager.UpdateStatus(_ProductId, ProductStatusEnum.Pausing);
                ProductStaticHtmlCache.Delete(_ProductId);
            }
            else if (opType == "3")
            {//删除 
                actionResult.success = ProductManager.UpdateStatus(_ProductId, ProductStatusEnum.Deleted);
                ProductStaticHtmlCache.Delete(_ProductId);
            }
            else
            {
                actionResult.success = false;
                actionResult.message = "无法确定操作类型";
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}